cmake_minimum_required (VERSION 3.15)
# 在项目中使用VERSION关键字时
if(POLICY CMP0048)
  cmake_policy(SET CMP0048 NEW)
endif()
project (Tutorial VERSION 1.0)

# 指定c++版本号
add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)

# 仅在通过生成此项目时添加编译器警告标志
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,GNU,LCC>")
set(msvc_cxx "$COMPILE_LANG_AND_ID:CXX,MSVC>")
target_compile_options(tutorial_compiler_flags INTERFACE
  "$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>>"
  "$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
)

# 让MathFunctions库可以作为可选项.
option(USE_MYMATH "Use tutorial provided math implementation" ON)

if (USE_MYMATH)
	# add the MathFunctions library
	add_subdirectory(MathFunctions)
	list(APPEND EXTRA_LIBS MathFunctions)
endif()

# 制定一个头文件来将版本号传递到源码里
configure_file(TutorialConfig.h.in TutorialConfig.h)

# 添加可执行文件
add_executable (Tutorial tutorial.cpp)

# --要先写add_executable
# 链接库目标到可执行目标
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS} tutorial_compiler_flags)

# 指定的文件会被写入二进制结构中,
# 我们必须将这一目录添加到搜索include文件的列表中.
target_include_directories(Tutorial PUBLIC
							"${PROJECT_BINARY_DIR}"
							)